package com.ruoyi.TaskForce.controller;

import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.TaskForce.domain.Team_Members;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.TaskForce.domain.TeamManagement;
import com.ruoyi.TaskForce.service.ITeamManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * TaskForceController
 * 
 * @author ruoyi
 * @date 2024-10-09
 */
@RestController
@RequestMapping("/Task Force/TaskForce")
public class TeamManagementController extends BaseController
{
    @Autowired
    private ITeamManagementService teamManagementService;

    /**
     * 查询TaskForce列表
     */
    @PreAuthorize("@ss.hasPermi('Task Force:TaskForce:list')")
    @GetMapping("/list")
    public TableDataInfo list(TeamManagement teamManagement)
    {
        startPage();
        List<TeamManagement> list = teamManagementService.selectTeamManagementList(teamManagement);
        return getDataTable(list);
    }

    /**
     * 导出TaskForce列表
     */
    @PreAuthorize("@ss.hasPermi('Task Force:TaskForce:export')")
    @Log(title = "TaskForce", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeamManagement teamManagement)
    {
        List<TeamManagement> list = teamManagementService.selectTeamManagementList(teamManagement);
        ExcelUtil<TeamManagement> util = new ExcelUtil<TeamManagement>(TeamManagement.class);
        util.exportExcel(response, list, "TaskForce数据");
    }

    /**
     * 获取TaskForce详细信息
     */
    @PreAuthorize("@ss.hasPermi('Task Force:TaskForce:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(teamManagementService.selectTeamManagementById(id));
    }
    /**
     * 根据id查询对应队伍
     */
    @PreAuthorize("@ss.hasPermi('Teams:Teams:query')")
    @GetMapping(value = "/getTeamsByTaskForceId/{id}")
    public AjaxResult getTeamsByTaskForceId(@PathVariable("id") Long id)
    {

        return success(teamManagementService.getTeamsByTaskForceId(id));
    }

    /**
     * 新增TaskForce
     */
    @PreAuthorize("@ss.hasPermi('AddTeam:AddTeam:add')")
    @Log(title = "AddTeam", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeamManagement teamManagement)
    {
        return toAjax(teamManagementService.insertTeamManagement(teamManagement));
    }

    /**
     * 新增队伍
     */

    @PreAuthorize("@ss.hasPermi('Task Force:TaskForce:add')")
    @Log(title = "TaskForce", businessType = BusinessType.INSERT)
    @PostMapping("/AddTeam")
    public AjaxResult AddTeam(@RequestBody List<Team_Members> team_members)
    {

        // 遍历 team_members 列表
        Optional<Team_Members> captain = team_members.stream()
                .filter(member -> "队长".equals(member.getPosition())) // 找到 position 为 "队长" 的成员
                .findFirst(); // 取第一个

        if (captain.isPresent()) {
            // 如果找到了队长，提取 phone_number 和 name 和 Position
            Team_Members captainMember = captain.get();
            String phone_number = captainMember.getPhone_number();
            String name = captainMember.getName();
            Long team_id = captainMember.getTeam_id();

            // 将队长信息保存到数据库（具体实现根据业务需求调整）
             teamManagementService.saveCaptainInfo(phone_number, name, team_id);

            // 继续插入其他成员信息

        }

       return toAjax(teamManagementService.insertTeamMembers(team_members));
    }

    /**
     * 修改TaskForce
     */
    @PreAuthorize("@ss.hasPermi('Task Force:TaskForce:edit')")
    @Log(title = "TaskForce", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeamManagement teamManagement)
    {
        return toAjax(teamManagementService.updateTeamManagement(teamManagement));
    }

    /**
     * 删除TaskForce
     */
    @PreAuthorize("@ss.hasPermi('Task Force:TaskForce:remove')")
    @Log(title = "TaskForce", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(teamManagementService.deleteTeamManagementByIds(ids));
    }


//    删除队伍人员
    @PreAuthorize("@ss.hasPermi('Task Force:TaskForce:remove')")
    @Log(title = "TaskForce", businessType = BusinessType.DELETE)
    @DeleteMapping("/DeleteEmbers/{id}")
    public AjaxResult RemoveMbers(@PathVariable Long id){
        return toAjax(teamManagementService.DeleteTeamMembers(id));
    }

//    获取片区 GetAreaName

    @PreAuthorize("@ss.hasPermi('Task Force:TaskForce:query')")
    @GetMapping(value = "/GetAreaName")
    public AjaxResult GetAreaName()
    {
        return success(teamManagementService.selectAreaName());
    }


}
